#include "mainw.h"
#include "ui_mainw.h"
#include<QDebug>

MainW::MainW(QWidget *parent):
   QMainWindow(parent),
 ui(new Ui::MainW)
{
    ui->setupUi(this);
    port=new QtSerialPort;
    port->open("/dev/ttyUSB0");
    port->start();
    connect(port,SIGNAL(readyRecv()),this,SLOT(showData()));
    fd=new QTcpSocket();
    file=new QFile("./ip.txt");
    file->open(QIODevice::ReadWrite | QIODevice::Text);
    QByteArray buf=file->readLine();
    buf.remove(buf.size()-1,1);
    qDebug()<<buf;
    QByteArray buf1=file->readLine();
    buf1.remove(buf1.size()-1,1);
    qDebug()<<buf1.toInt();
    fd->connectToHost(buf,buf1.toInt());
    connect(fd,SIGNAL(connected()),this, SLOT(handelConnected()));////查看是否连接成功*/
}
MainW::~MainW()
{
    delete ui;
}
void MainW::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
void MainW::showData()
{
    QString str;
    str= port->receive();
    qDebug()<<str<<'\n';
    list=str.split("|",QString::SkipEmptyParts);
    if(list.size()==4)
    {
        ui->lineEdit->setText(list.at(0));
        ui->lineEdit_2->setText(list.at(1));
        ui->lineEdit_3->setText(list.at(2));
        ui->lineEdit_4->setText(list.at(3));
        QString str1="#3|"+str;
        fd->write(str1.toAscii());
     }
}
void MainW::handelConnected()
{
    connect(fd,SIGNAL(readyRead()),this,SLOT(handleRead()));//查看这条连接是否可读
}
void MainW::handleRead()
{
	QByteArray buf=fd->readLine();
	port->send(buf.data(),2);
}
void MainW::on_pushButton_5_clicked()
{
}
void MainW::on_pushButton_7_clicked()
{
    char buf[3]={'0','#','\0'};
    port->send(buf,2);

     qDebug()<<buf;
}
void MainW::on_pushButton_6_clicked()
{
    char buf[3]={'1','#','\0'};
    port->send(buf,2);
    qDebug()<<buf;
}
